﻿namespace Sage.Http.Authentication
{
    /// <summary>
    /// 自定义认证提供者，使用委托实现认证逻辑
    /// </summary>
    /// <remarks>
    /// 初始化自定义认证提供者
    /// </remarks>
    /// <param name="authenticator">认证委托</param>
    public class CustomAuthProvider(Func<HttpRequestMessage, Task> authenticator) : IAuthenticationProvider
    {
        private readonly Func<HttpRequestMessage, Task> _authenticator = authenticator ?? throw new ArgumentNullException(nameof(authenticator));

        /// <summary>
        /// 应用自定义认证
        /// </summary>
        /// <param name="request">HTTP请求</param>
        public Task AuthenticateRequest(HttpRequestMessage request)
        {
            ArgumentNullException.ThrowIfNull(request);

            return _authenticator(request);
        }
    }
}
